/**
 * Copyright 2024/3/24 the original brandnewdata.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zlb.envmodify.example;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 此案例将UnmodifiableMap类变成可修改的，以解决Spring中无法动态修改配置的问题
 * @ClassName ChangeUnmodifiableMap
 * @Author zhulb
 * @Date 2024/3/24 13:46
 * @Version 0.0.1
 */
public class ChangeUnmodifiableMap {
    /**
     * 效果
     * 原来：方法不打印任何输出
     * 插件：使用插件后，将输出给定的字符串
     */
    public static void main(String[] args) {
        /**
         * 效果
         * 原来：代用put会报错
         * 插件：使用插件后，put成功，并且能打印出新的值
         */
        Map<Object, Object> unmodifiableMap = Collections.unmodifiableMap(new HashMap<>());
        unmodifiableMap.put("zhulb","44");
        System.out.println(unmodifiableMap.get("zhulb"));

        /**
         * SingletonMap的put方法没有重写，调用的是AbstractMap.put方法，故会直接报错
         */
        Map<Object,Object> unmodifiableMap2 = Collections.unmodifiableMap(Collections.singletonMap("zhulb","33"));
        unmodifiableMap2.put("zhulb","44");
    }

}
